home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / TRUENAME.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  2KB  |  104 lines

  1. /*
  2. ** Apologies for the grotty code; I only just whipped this up.
  3. **
  4. ** tname.c -- wrapper for the undocumented DOS function TRUENAME
  5. **
  6. ** TRUENAME: interrupt 0x21 function 0x60
  7. **
  8. **   Call with: ah    =  60h
  9. **              es:di -> destination buffer
  10. **              ds:si -> source buffer
  11. **
  12. **   Returns:   carry bit set if there were problems
  13. **
  14. ** This code hereby contributed to the public domain.
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <dos.h>
  22.  
  23. #ifdef __TURBOC__
  24.  #define _far far
  25. #endif
  26.  
  27. /*
  28. ** Strip leading and trailing blanks from a string.
  29. */
  30.  
  31. char _far *strip(char _far *s)
  32. {
  33.       char _far *end;
  34.  
  35.       for ( ; isspace(*s); s++)
  36.             ;
  37.  
  38.       for (end = s; *end; end++)
  39.             ;
  40.  
  41.       for (end--; isspace(*end); *end-- = '\0')
  42.             ;
  43.  
  44.       return s;
  45. }
  46.  
  47. /*
  48. ** Truename itself. Note that I'm using intdosx() rather than
  49. ** playing with some inline assembler -- I've discovered some
  50. ** people that actually don't have an assembler, poor bastards :-)
  51. */
  52.  
  53. char _far *truename(char _far *dst, char _far *src)
  54. {
  55.       union REGS rg;
  56.       struct SREGS rs;
  57.  
  58.       if (!src || !*src || !dst)
  59.             return NULL;
  60.  
  61.       src=strip(src);
  62.  
  63.       rg.h.ah=0x60;
  64.       rg.x.si=FP_OFF(src);
  65.       rg.x.di=FP_OFF(dst);
  66.       rs.ds=FP_SEG(src);
  67.       rs.es=FP_SEG(dst);
  68.  
  69.       intdosx(&rg,&rg,&rs);
  70.  
  71.       return (rg.x.cflag) ? NULL : dst;
  72. }
  73.  
  74. #ifdef TEST
  75.  
  76. /*
  77. ** ... and a little test function.
  78. */
  79.  
  80. int main(int argc, char *argv[])
  81. {
  82.       char buf[128]="                             ", _far *s;
  83.       int i;
  84.  
  85.       if (3 > _osmajor)
  86.       {
  87.             puts("Only works with DOS 3+");
  88.             return -1;
  89.       }
  90.       if(argc > 1)
  91.       {
  92.             for(i = 1; i < argc; i++)
  93.             {
  94.                   s = truename((char _far *)buf,(char _far *)argv[i]);
  95.                   printf("%s=%s\n",argv[i], s ? buf : "(null)");
  96.             }
  97.       }
  98.       else  printf("Usage: TRUENAME [filename [filename...]]\n");
  99.  
  100.       return 0;
  101. }
  102.  
  103. #endif
  104.